#include "head.h"

/***************************************************************************************
 *                                注册、登入、退出								    *
 ***************************************************************************************/
/*
 * @Author:yhy
 * @LastAuthor:yhy
 */
bool sign_in(int net_fd, user_t* user){
    printf("请输入用户名：");
    fflush(stdout);
    char userName[1024];
    int readNum = read(STDIN_FILENO,userName,sizeof(userName));
    userName[readNum-1] = '\0';
    printf("输入密码: ");
    fflush(stdout);
    char passWord[1024];
    readNum = read(STDIN_FILENO,passWord,sizeof(passWord));
    passWord[readNum-1] = '\0';
//printf("%s1 :%s2\n", userName, passWord);
    off_t username_len = strlen(userName);
    off_t password_len = strlen(passWord);
//printf("%d: %d\n", username_len, password_len);

    send(net_fd, &username_len, sizeof(off_t), MSG_NOSIGNAL);
    send(net_fd, userName, username_len, MSG_NOSIGNAL);
    send(net_fd, &password_len, sizeof(off_t), MSG_NOSIGNAL);
    send(net_fd, passWord, password_len, MSG_NOSIGNAL);

    printf("正在验证账号密码...\n");

    sleep(1);
    int type = 0;
    recv(net_fd, &type, sizeof(int), MSG_WAITALL);
    if(type == 0){
        printf("登入成功\n");
        strcpy(user->user_name, userName);
        //printf("user_name: %s\n", user->user_name);
        off_t id_len = 0;
        char id[12] = {0};
        recv(net_fd, &id_len, sizeof(off_t), MSG_WAITALL);
        recv(net_fd, id, id_len, MSG_WAITALL);
//printf("%s\n", id);
        strcpy(user->user_id, id);
        user->path[0] = '/';

        char* p1 = user->path + 1;
        char* p2 = userName;
        while(*p1++ = *p2++)
            ;
        
    }else{
        printf("账号或密码错误，请重新登录！\n");
        return false;
    }
    return true;
}
bool sign_up(int socket_fd){

    int ret_read;
    int type = 0;
    char userName[512] = {0};
    while(1){
        memset(userName, 0, sizeof(userName));
        printf("请输入用户名：");
        fflush(stdout);

        ret_read = read(STDIN_FILENO, userName, sizeof(userName));
        if(ret_read == -1){
            //TODO: 日志
            fflush(stdin);
            printf("未识别到输入，请重新输入用户名！\n");
        }
        userName[strlen(userName)-1] = '\0';

        off_t username_len = strlen(userName);
        send(socket_fd, &username_len, sizeof(off_t), MSG_NOSIGNAL);
        send(socket_fd, userName, username_len, MSG_NOSIGNAL);

        recv(socket_fd, &type, sizeof(int), MSG_WAITALL);
        if(type == 1){

            printf("用户名已存在，请重新输入用户名！\n");
            
        }else if(type == -1){
            printf("注册失败，请重新输入用户名！\n");
        }else{
            break;
        }


    }
    char passWord[64];
    bool flag = false;
    while(!flag){
        memset(passWord, 0, sizeof(passWord));

        printf("请输入12位以内密码: ");
        fflush(stdout);
        fflush(stdin);
        ret_read = read(STDIN_FILENO, passWord, sizeof(passWord));
        if(ret_read > 13){
            printf("密码格式不正确，请重新输入！\n");
            continue;
        }
        passWord[ret_read-1] = '\0';


        char confpassWord[1024] = {0};

        printf("请确认密码: ");
        fflush(stdout);
        ret_read = read(STDIN_FILENO, confpassWord, sizeof(confpassWord));
        confpassWord[ret_read-1] = '\0';
        //printf("确认密码不正确，请重新输入！\n");
        while(strcmp(passWord, confpassWord) != 0){

            memset(confpassWord, 0, sizeof(confpassWord));

            printf("确认密码: ");
            fflush(stdout);

            ret_read = read(STDIN_FILENO, confpassWord, sizeof(confpassWord));
            confpassWord[ret_read-1] = '\0';
        }

        break;

    }
    off_t password_len = strlen(passWord);
    send(socket_fd, &password_len, sizeof(off_t), MSG_NOSIGNAL);
    send(socket_fd, passWord, password_len, MSG_NOSIGNAL);
//printf("%ld: %s", password_len, passWord);
    recv(socket_fd, &type, sizeof(int), MSG_WAITALL);
    if(type == 0){
        printf("注册成功！\n");
    }

    return true;
}

